package com.schedule.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.varia.StringMatchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.schedule.entity.User;
import com.schedule.service.UserService;

@Controller
public class UserController {
	@Autowired
	UserService userService;
	
	
	@ResponseBody
	@RequestMapping(value="/addUser.action",produces="text/json;charset=UTF-8")
	public Object add(@RequestParam String param) { 
		//把注册信息添加到表里
		Gson gson=new Gson();
		Map<String, Object> result=new HashMap<String, Object>();
		User user=gson.fromJson(param,new TypeToken<User>(){}.getType()); 
		
		String address=user.getAddress();
		List<User> list=userService.FindAll();
		int temp=1;
		
		for(int i=0;i<list.size();i++) {
			if(address.equals(list.get(i).getAddress())) {
				result.put("code", -1);
				result.put("data", "邮箱已存在");
				temp=0;
			}
		}
		
		if(temp==1) {
			result.put("code", 1);
			result.put("data", "注册成功");
			userService.AddUser(user);
		}
	
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/findPerson.action",produces="text/json;charset=UTF-8")
	public Object findPerson(@RequestParam String user) {
		//查找用户的个人信息
		User u=userService.FindPerson(user);
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("code", 1);
		result.put("data", u);
		System.out.println(u.getAvatar());
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/findByname.action",produces="text/json;charset=UTF-8")
	public Object findByname(@RequestParam String param) {
		//根据姓名搜索好友列表的某个人
		Gson gson=new Gson();
		User user=gson.fromJson(param,new TypeToken<User>(){}.getType()); 
		String name=user.getName();
		String user1=user.getUser();
		
		List<User> list=userService.FindByname(name, user1);

		Map<String, Object> result=new HashMap<String, Object>();
		
		int len=list.size();
		if(len==0) {
			result.put("code", 0);
			result.put("data", "没有搜到该好友");
		}else {
			result.put("code", 1);
			result.put("data", list);
		}
		
		return new Gson().toJson(result);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/findByphone.action",produces="text/json;charset=UTF-8")
	public Object findByphone(@RequestParam String param) {
		//根据手机搜索好友列表的某个人
		Gson gson=new Gson();
		User user=gson.fromJson(param,new TypeToken<User>(){}.getType()); 
		String phone=user.getPhone();
		String user1=user.getUser();
		
		User list=userService.FindByPhone(phone, user1);

		Map<String, Object> result=new HashMap<String, Object>();
		
		
		if(list==null) {
			result.put("code", 0);
			result.put("data", "没有搜到该好友");
		}else {
			result.put("code", 1);
			result.put("data", list);
		}
		
		return new Gson().toJson(result);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addByphone.action",produces="text/json;charset=UTF-8")
	public Object AddByphone(@RequestParam String phone) {
		//根据手机搜索好友列表的某个人
        User u=userService.AddByPhone(phone);
        
		Map<String, Object> result=new HashMap<String, Object>();
		
		if(u==null) {
			result.put("code", 0);
			result.put("data", "没有搜到该用户");
		}else {
			result.put("code", 1);
			result.put("data",u);
		}
		
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/rememberPsw.action",produces="text/json;charset=UTF-8")
	public Object RememberPsw(@RequestParam String user) {
		//如果用户记住密码，则把密码显示在页面上
		System.out.println(user);
		User u=userService.FindPerson(user);
		Map<String, Object> result=new HashMap<String, Object>();
		if(u!=null) {
			int rem=u.getRemember();
			String psw=u.getPassword();
			if(rem==1) {
				result.put("code", 1);
				result.put("data", psw);
			}else {
				result.put("code", 0);
				result.put("data", null);
			}
		}else {
			result.put("code", -1);
			result.put("data", "用户名不存在");
		}
		
		
				
		
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/findPsw.action",produces="text/json;charset=UTF-8")
	public Object findPsw(@RequestParam String param) {
		//验证登录时输入密码与注册密码是否一致
		Map<String, Object> result=new HashMap<String, Object>();
		JsonParser jp=new JsonParser();
		String user=jp.parse(param).getAsJsonObject().get("user").getAsString();
		String psw=jp.parse(param).getAsJsonObject().get("password").getAsString();
		
		User u=userService.FindPerson(user);
		if(u==null) {
			result.put("code", 0);
			result.put("data", "用户名不存在");
		}else {
			if(psw.equals(u.getPassword())) {
				result.put("code", 1);
				result.put("data", "登录成功");
			}else {
				result.put("code", -1);
				result.put("data", "登录失败");
			}
		}
		
		
		return new Gson().toJson(result);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/updatePerson.action",produces="text/json;charset=UTF-8")
	public Object updatePerson(@RequestParam String param) {
		//更新用户个人信息
		Gson gson=new Gson();
		Map<String, Object> result=new HashMap<String, Object>();
		
		User user=gson.fromJson(param,new TypeToken<User>(){}.getType()); 
		
		userService.UpdatePerson(user);
		result.put("code", 1);
		
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/updateRemember.action",produces="text/json;charset=UTF-8")
	public Object updateRemember(@RequestParam String param) {
		//更新用户记住密码状态
		Gson gson=new Gson();
		Map<String, Object> result=new HashMap<String, Object>();
		
		User user=gson.fromJson(param,new TypeToken<User>(){}.getType()); 
		
		userService.UpdateRemember(user);
		result.put("code", 1);
		
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/findUser.action",produces="text/json;charset=UTF-8")
	public Object findUser(@RequestParam String user) {
		//验证用户名是否已存在
		
		Map<String, Object> result=new HashMap<String, Object>();
		List<User> list=userService.FindAll();
		int temp=1;
		for(int i=0;i<list.size();i++) {
			
			if(user.equals(list.get(i).getUser())) {
				result.put("code", -1);
				result.put("data", "用户名已存在");
				temp=0;
			}
		}
		
		if(temp==1) {
			result.put("code", 1);
			
		}
		
		return new Gson().toJson(result);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/findPhone.action",produces="text/json;charset=UTF-8")
	public Object findPhone(@RequestParam String phone) {
		//验证手机号是否存在
		Map<String, Object> result=new HashMap<String, Object>();
		List<User> list=userService.FindAll();
		int temp=1;
		
		for(int i=0;i<list.size();i++) {
			if(phone.equals(list.get(i).getPhone())) {
				result.put("code", -1);
				result.put("data", "手机号已存在");
				temp=0;
			}
		}
		
		if(temp==1) {
			result.put("code", 1);
		
		}
		
		return new Gson().toJson(result);
	}
	@RequestMapping("/updateAndUpload.action")
	public void updateAndUpload(HttpServletRequest request, HttpServletResponse response,MultipartFile avatar) throws IllegalStateException, IOException{
		String user = request.getParameter("user");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String password = request.getParameter("password");
		User users = new User();
		users.setUser(user);
		users.setName(name);
		users.setPhone(phone);
		users.setAddress(address);
		users.setPassword(password);
		if (!avatar.isEmpty()) {
            String path = "D:\\upload\\";
            String originalFileName = avatar.getOriginalFilename();
            // 新的图片名称
            String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
            // 新的图片
            File newFile = new File(path + newFileName);
            // 将内存中的数据写入磁盘
            avatar.transferTo(newFile);
            users.setAvatar(newFileName);
        }
		userService.UpdateAndUpload(users);
		response.sendRedirect("jsp/setinfo.jsp?code=1&user="+user);
	}
	
	

}
